package dataSQL;

import java.io.*;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * @author liyanhao
 * @Date 2024-04-25 10:30
 */
public class GenSQL {
    public static void main(String[] args) {
        String directoryPath = "D:\\IdeaProjects\\JavaTechnologyLearn\\tools\\src\\main\\java\\dataSQL\\addressbrand"; // 替换为你要遍历的文件夹路径
        String fileName = "D:\\IdeaProjects\\JavaTechnologyLearn\\tools\\src\\main\\java\\dataSQL\\outsql.sql";
        File file1 = new File(fileName);
        file1.delete();
        File directory = new File(directoryPath);
        File[] files = directory.listFiles((dir, name) -> name.endsWith(".txt")); // 可以根据需要调整过滤条件

        if (files != null) {
            for (File file : files) {
                processFileWithBufferedReader(file);
            }
        } else {
            System.err.println("No files found in directory: " + directoryPath);
        }
    }

    private static void processFileWithBufferedReader(File file) {
//        System.out.println("Processing file: " + file.getName());
        HashSet<String> set = new HashSet<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] split = line.split("    ");
                String shopName = split[0];
                boolean add = set.add(shopName);
                if (add) {
                    generateSQL(file.getName(), line);
                }
            }
        } catch (IOException e) {
            System.err.println("Error reading file " + file + ": " + e.getMessage());
            e.printStackTrace();
        }
    }
    //INSERT INTO `ods_jd_address` (`brand`, `province`, `city`, `area`, `mdlx`, `address`, `store_name`, `tel`, `create_time`, `data_sources`, `store_no`)
    // VALUES ( 'xiaomi', '北京', '北京市', '朝阳区', '专卖店', '北京市朝阳区曙光西里甲5号凤凰汇购物中心一层小米之家',
    // '小米之家北京朝阳凤凰汇专卖店', '18310227680', '2024-04-21 15:50:07', 0, 'JM20390');

    public static void generateSQL(String fileName, String str) {

        //宁波-华为手机.txt
        String city = fileName.substring(0, fileName.lastIndexOf("-"));
        String province = getProvince(city);
        String brand = fileName.substring(fileName.lastIndexOf("-") + 1, fileName.length()).replace("手机.txt", "");

        String[] split = str.split("    ");
        try {

            String shopName = split[0].replace("★", "").replaceAll("\\d", "").replaceAll("\\.", "");
            System.out.println(shopName);
            String address = split[1];
            String phone = split[2].replaceAll(" ", "");


            int index = address.indexOf("区");
            String area = address.substring(index - 3, index);
            if (area.charAt(0) == '市') {
                area = area.substring(1, area.length());
            }
            area = area + "区";

            String sql = "INSERT INTO `ods_jd_address` (`brand`, `province`, `city`, `area`, `mdlx`, `address`, `store_name`, `tel`, `create_time`, `data_sources`, `store_no`)\n" + "                VALUES ( '" + brand + "', '" + province + "', '" + city + "市', '" + area + "', '', '" + address + "',\n" + "                '" + shopName + "', '" + phone + "', now(), 1, '');\n\n";
            extracted(sql);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }


    public static String getProvince(String city) {
        HashMap<String, String> map = new HashMap<>();
        map.put("上海", "上海");
        map.put("南通", "江苏");
        map.put("镇江", "江苏");
        map.put("常州", "江苏");
        map.put("无锡", "江苏");
        map.put("苏州", "江苏");
        map.put("南京", "江苏");
        map.put("杭州", "浙江");
        map.put("宁波", "浙江");
        map.put("温州", "浙江");
        map.put("舟山", "浙江");
        map.put("台州", "浙江");
        map.put("丽水", "浙江");
        map.put("郑州", "河南省");
        map.put("西安", "陕西省");
        map.put("成都", "四川省");
        return map.get(city);
    }
    private static void extracted(String contentToAppend) {
        String fileName = "D:\\IdeaProjects\\JavaTechnologyLearn\\tools\\src\\main\\java\\dataSQL\\outsql.sql";
        try (FileWriter writer = new FileWriter(fileName, true)) {
            writer.write(contentToAppend);
        } catch (IOException e) {
            System.err.println("An error occurred while appending content to the file: " + e.getMessage());
            e.printStackTrace();
        }
    }

}




